package com.atguigu.paymentdemo.service.impl;

import com.atguigu.paymentdemo.entity.CourseFile;
import com.atguigu.paymentdemo.mapper.CourseFileMapper;
import com.atguigu.paymentdemo.service.CourseFileService;
import com.atguigu.paymentdemo.util.AliyunOssUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author wulifeng
 */
@Service
public class CourseFileServiceImpl extends ServiceImpl<CourseFileMapper, CourseFile> implements CourseFileService {

    @Override
    public Optional<CourseFile> getCourseImage(Serializable infoId) {
        LambdaQueryWrapper<CourseFile> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CourseFile::getInfoId, infoId);
        wrapper.eq(CourseFile::getFileType, "1");
        return this.baseMapper.selectList(wrapper).stream().findFirst();
    }

    @Override
    public List<CourseFile> getImage(Serializable infoId) {
        LambdaQueryWrapper<CourseFile> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CourseFile::getInfoId, infoId);
        wrapper.eq(CourseFile::getFileType, "1");
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<CourseFile> getVideo(Serializable infoId) {
        LambdaQueryWrapper<CourseFile> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CourseFile::getInfoId, infoId);
        wrapper.eq(CourseFile::getFileType, "2");
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public void removeByInfoId(Serializable infoId) {
        List<String> keys;
        LambdaQueryWrapper<CourseFile> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CourseFile::getInfoId, infoId);

        List<CourseFile> list = this.baseMapper.selectList(wrapper);

        keys = list.stream().map(CourseFile::getFilePath).collect(Collectors.toList());
        AliyunOssUtil.deleteFiles(keys);

        System.out.println("removeByInfoId:课程删除时 oss文件一并删除");
    }

    @Override
    public void removeByInfoIds(Collection<? extends Serializable> infoIds) {
        List<String> keys;
        LambdaQueryWrapper<CourseFile> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CourseFile::getInfoId, infoIds);

        List<CourseFile> list = this.baseMapper.selectList(wrapper);

        keys = list.stream().map(CourseFile::getFilePath).collect(Collectors.toList());
        AliyunOssUtil.deleteFiles(keys);

        System.out.println("removeByInfoIds:课程删除时 oss文件一并删除");
    }

    @Override
    public boolean removeById(Serializable id) {
        //联动删除oss文件
        List<String> keys = new ArrayList<>();
        keys.add(this.baseMapper.selectById(id).getFilePath());
        AliyunOssUtil.deleteFiles(keys);

        System.out.println("removeById:课程删除时 oss文件一并删除");
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        //联动删除oss文件
        List<String> keys;
        List<CourseFile> list = this.baseMapper.selectBatchIds(idList);
        keys = list.stream().map(CourseFile::getFilePath).collect(Collectors.toList());
        AliyunOssUtil.deleteFiles(keys);
        System.out.println("removeByIds:课程删除时 oss文件一并删除");
        /*LambdaQueryWrapper<CourseFile> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CourseFile::getId,idList);

        this.baseMapper.selectList(wrapper);*/

        return super.removeByIds(idList);
    }
}
